package com.aistar.service.user.impl;

import com.aistar.mapper.EmployeeMapper;
import com.aistar.mapper.UserMapper;
import com.aistar.model.entity.User;
import com.aistar.service.user.IPasswordChangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class PasswordChangeServiceImpl implements IPasswordChangeService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Integer passwordChange(Integer employeeId,String oldPassword,String newPassword,String newPasswordAgain) {
        int count = -1;
        if (userMapper.findById(employeeMapper.findById(employeeId).getUserId()).getPassword().equals(oldPassword)){
            count = 0;
            if (!oldPassword.equals(newPassword)){
                count = 1;
                if (!newPassword.equals(newPasswordAgain)){
                    User user = userMapper.findById(employeeMapper.findById(employeeId).getUserId());
                    user.setPassword(newPassword);
                    count+=userMapper.updateByid(user);
                }
            }
        }
        return count;
    }
}
